# -*- coding: utf-8 -*-
"""
Created on Thu Mar 16 11:00:03 2023

@author: maomao
"""

import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.api as sm
from statsmodels.graphics.tsaplots import plot_predict
from statsmodels.tsa.arima.model import ARIMA
import numpy as np
from pandas import DataFrame
from  statsmodels.tsa.stattools  import  adfuller  as  ADF


#dta = sm.datasets.sunspots.load_pandas().data[['SUNACTIVITY']]
dta =  [10.39151217,11.78738157,12.38719759,14.01208827,15.68135656,20.69874104,22.50876956,23.26573736,24.13639162,27.18005913,28.23390824,31.0895749,30.71170319,36.36174018,37.24073882,38.06726183]

dta=pd.Series(dta)
print(u'原始序列的ADF检验结果为：',  ADF(dta))
dta.index = pd.date_range(start='2005', end='2021', freq='A')
res = ARIMA(dta, order=(2,2,0)).fit()
print(res.summary())

fig, ax = plt.subplots()
# 设置刻度线朝内
ax.xaxis.set_tick_params(direction='in')
ax.yaxis.set_tick_params(direction='in')
ax = dta.loc['2006':].plot(ax=ax)
#plt.xlim(xmax=2030, xmin=2000)
plt.ylim(ymax=100, ymin=10)

#plt.plot(res.forecast(10), color='blue')
plot_predict(res, '2014', '2050', ax=ax)
plt.xlabel('Year',fontsize=12,fontname='Times New Roman',verticalalignment='top')
plt.ylabel('Carbon emission[MtCO2]',fontname='Times New Roman',fontsize=12,horizontalalignment='center')
plt.savefig('E:/materials/500dpiARIMA(2,2,0).jpg', dpi=500, bbox_inches='tight')
plt.show()
s=res.forecast(30) 
print (s)#4273.45表明将在2032年实现碳达峰。仍需要努力，和我国对世界做出的承诺还有一定差距。
#https://py4u.org/questions/73112516/

